﻿using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using TrackSystem.Contracts;
using TrackSystem.Entities.Dtos.Equip;
using TrackSystem.Entities.Dtos.Logic;
using TrackSystem.Entities.Dtos.Loop;
using TrackSystem.Entities.Extensions;
using TrackSystem.Entities.Models;
using TrackSystem.Entities.RequestFeatures;

namespace TrackSystem.API.SignalRHub
{
    /// <summary>
    /// 基于SignalR的逻辑表达式处理Hub
    /// </summary>
    /// <param name="wrapper"></param>
    /// <param name="mapper"></param>
    public class LoopHelperHub(IRepositoryWrapper wrapper, IMapper mapper) : Hub
    {

        /// <summary>
        /// 获取所有逻辑表达式
        /// </summary>
        /// <returns></returns>
        public async Task GetAllAsync()
        {
            var entities = await wrapper.Loop.GetAllListAsync();
            var dtos = mapper.Map<IEnumerable<LoopDto>>(entities);
            await Clients.Caller.SendAsync("ReceiveAllLoop", dtos);
        }

        /// <summary>
        /// 获得单个逻辑表达式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task GetSingleAsync(int id)
        {
            var entities = await wrapper.Loop.GetSingle(id);
            var dto = mapper.Map<LoopDto>(entities);
            await Clients.Caller.SendAsync("RecvSingle", dto);
        }

        /// <summary>
        /// 分页查询逻辑表达式
        /// </summary>
        /// <param name="parameter">分页参数</param>
        /// <returns></returns>
        public async Task GetPagedLoops(RequestParameter parameter)
        {
            var entities = wrapper.Loop.GetPagedLoops(parameter);
            var dtos = mapper.Map<IEnumerable<LoopDto>>(entities);
            await Clients.Caller.SendAsync("ReceivePagedLoops", dtos, entities.MetaData.CurrentPage, entities.MetaData.TotalPages);
        }

        /// <summary>
        /// 获取逻辑表达式的最大Id
        /// </summary>
        /// <returns></returns>
        public async Task GetMaxId()
        {
            var vars = await wrapper.Loop.GetAllListAsync();
            var max = vars.Any() ? vars.Max(a => a.Id) : 0;
            await Clients.Caller.SendAsync("RecvMaxId", max);
        }

        /// <summary>
        /// 更新逻辑表达式
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateLoopAsync(LoopDto dto)
        {
            var item = mapper.Map<LoopTable>(dto);
            var entities = await wrapper.Loop.UpdateLoopAsync(item);
            if (entities?.Success ?? false)
            {
                await wrapper.Var.UpdateBaseLoop(mapper.Map<LoopTable>(entities.Data));
            }
            await Clients.Caller.SendAsync("RecvUpdateLoop", entities);
        }

        /// <summary>
        /// 新增逻辑表达式
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task InsertLoopAsync(LoopDto dto)
        {
            var item = mapper.Map<LoopTable>(dto);
            var entities = await wrapper.Loop.InsertLoopAsync(item);
            if (entities != null)
            {
                await wrapper.Var.UpdateBaseLoop(mapper.Map<LoopTable>(entities.Data));
            }
            await Clients.Caller.SendAsync("RecvInsertLoop", entities);
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        public Task GetEquipsAsync()
        {
            var entities = wrapper.Equip.GetAll();
            var equipDtos = mapper.Map<IEnumerable<EquipDto>>(entities);
            return Clients.Caller.SendAsync("RecvEquips", equipDtos);
        }

        /// <summary>
        /// 删除逻辑表达式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteLoopAsync(int id)
        {
            var result = await wrapper.Loop.DeleteLoopAsync(id);
            if (result?.Success ?? false)
            {
                await wrapper.Var.DeleteBasLoop(id);
            }
            await Clients.Caller.SendAsync("RecvDeleteLoop", result);
        }

        /// <summary>
        /// 获取所有的变量名称
        /// </summary>
        /// <returns></returns>
        public async Task GetVarNames()
        {
            var regs = await wrapper.Var.GetAllListAsync();
            var names = regs.Select(reg => reg.Name).ToList();
            await Clients.Caller.SendAsync("RecvVarNames", names);
        }

        /// <summary>
        /// 获取所有的整型或浮点变量名称
        /// </summary>
        public async Task GetPVNames()
        {
            var regs = await wrapper.Var.GetAllListAsync();
            var names = regs.Where(a => 
                a.DataType == DataTypeEnum.INTEGER || a.DataType == DataTypeEnum.DOUBLE)
                .Select(reg => reg.Name).ToList();
            await Clients.Caller.SendAsync("RecvPVNames", names);
        }

        /// <summary>
        /// 获取所有的整型或浮点而且可写入的变量名称
        /// </summary>
        /// <returns></returns>
        public async Task GetSVNames()
        {
            var regs = await wrapper.Var.GetAllListAsync();
            var names = regs.Where(a =>
                (a.DataType == DataTypeEnum.INTEGER || a.DataType == DataTypeEnum.DOUBLE)
                && a.ReadOnly == false)
                .Select(reg => reg.Name).ToList();
            await Clients.Caller.SendAsync("RecvSVNames", names);
        }
    }
}

